import React, {useEffect, useState} from 'react';
import './index.css'
import Html from "../Html";
import http from "../../../util/http";
import yd from "../Academic/image/yd.svg";
import {useLocation, useNavigate} from "react-router-dom";

function useTeamData() {
    const navigate = useNavigate();
    const location = useLocation();
    const state = location.state || {};
    const { page, text } = state;
    const [list1, setList1] = useState([]);
    const [list2, setList2] = useState([]);
    const [list3, setList3] = useState([]);
    const [list4, setList4] = useState([]);

    useEffect(() => {
        http.doGet('/doc/getRoleTeam', {}).then(res => {
            if (res.status === 200) {
                const data = res.data;
                const l1 = [];
                const l2 = [];
                const l3 = [];
                const l4 = [];
                if (data) {
                    data.forEach((item) => {
                        if (item.lx == 0) {
                            l1.push(item);
                        } else if (item.lx == 1) {
                            l2.push(item);
                        } else if (item.lx == 2) {
                            l3.push(item);
                        } else if (item.lx == 3) {
                            l4.push(item);
                        }
                    });
                }
                setList1(l1);
                setList2(l2);
                setList3(l3);
                setList4(l4);
            }
        });
    }, []);

    const openWord = (docId) => {
        if (docId && docId !== "") {
            navigate('/portal/details?hiddenLeft=1', {
                state: { docId, page: page, text: text }
            });
        }
    };

    return { list1, list2,list3,list4,openWord };
}

// 排序函数
function sortBySx(currList) {
    return currList.sort((a, b) => {
        const aIsNum = !isNaN(parseFloat(a.sx)) && isFinite(a.sx);
        const bIsNum = !isNaN(parseFloat(b.sx)) && isFinite(b.sx);

        if (aIsNum && bIsNum) {
            return parseInt(a.sx) - parseInt(b.sx);
        } else if (aIsNum && !bIsNum) {
            return -1;
        } else if (!aIsNum && bIsNum) {
            return 1;
        } else {
            return 0;
        }
    });
}


function TalentTeamBase({ docId, Component,type }) {
    const { list1,list2,list3,list4, openWord } = useTeamData();
    let currList = []
    if (type == 0){
        currList = list1
    }else if (type == 1){
        currList = list2
    }else if (type == 2){
        currList = list3
    }else if (type == 3){
        currList = list4
    }
    if (currList instanceof Array){
        debugger
        currList = sortBySx(currList)
    }else {
        currList = []
    }

    debugger
    return (
        <div className="talent-team">
            <div>
                <Html params={{ docId }} />
                {
                    (type == 1 || type == 2 || type ==3 ) && <div>
                        <div className="talent-team-title">团队介绍：</div>
                        <div className="talent-team-list">
                            {

                                currList && currList.map((item, index) => (
                                    <div className="talent-team-list-one" onClick={() => openWord(item.wd)} key={index}>
                                        <div className="talent-team-list-one-img">
                                            <div className="circle-bullet blue-dark"></div>
                                        </div>
                                        <div className="talent-team-list-one-text">
                                            {item.wdmc}
                                        </div>
                                    </div>
                                ))
                            }
                        </div>
                    </div>
                }

            </div>
        </div>
    );
}

// 人才队伍
function TalentTeam(props) {
    return <TalentTeamBase docId={392} {...props} type={0}/>;
}

// 揭榜挂帅团队
function TalentTeamNew(props) {
    return <TalentTeamBase docId={351160} {...props} type={1}/>;
}

// 团队建设情况
function TeamBuilder(props) {
    debugger
    return <TalentTeamBase docId={286099} {...props} type={2}/>;
}

// 人才创新团队
function InnovationTeam(props) {
    return <TalentTeamBase docId={351158} {...props} type={3}/>;
}

export {TalentTeam, TalentTeamNew, TeamBuilder, InnovationTeam};